package com.ctg.itrdc.imageconsole.data.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.ctg.itrdc.imageconsole.data.dao.IClusterConfigDao;
import com.ctg.itrdc.imageconsole.data.dao.IK8sAlertDao;
import com.ctg.itrdc.imageconsole.data.dto.K8sAlertInfoDTO;
import com.ctg.itrdc.paspsdk.data.dao.impl.BaseDaoImpl;

@Component("k8sAlertDao")
public class K8sAlertDaoImpl extends BaseDaoImpl implements IK8sAlertDao {

	@Resource
	private IClusterConfigDao clusterConfigDao;

	@Override
	public List<K8sAlertInfoDTO> queryAlertInfosByClusterCodes(List<String> clusterCodes) {
		StringBuffer sqlsb = new StringBuffer("select id,insert_time,cluster_i_d as cluster_code,level,alert_type,namespace,"
				+ "content,per_num,rule,message,is_active from alert_info_table "
				+ "where is_active=1 and cluster_i_d in (");
		
		for(int i=0,len=clusterCodes.size();i<len;i++) {
			sqlsb.append("'"+ clusterCodes.get(i) + "'");
			if (i<(len-1)) {
				sqlsb.append(",");
			}
		}
		sqlsb.append(") order by insert_time desc limit 0,10");

		return this.jdbcFindList(sqlsb.toString(), K8sAlertInfoDTO.class, new ArrayList<>());
	}

	@Override
	public Long getAlertCountByClusterCodes(List<String> clusterCodes) {
		StringBuffer sqlsb = new StringBuffer("");
		sqlsb.append("select count(1) as count from alert_info_table where is_active=1 "
				+ "and cluster_i_d in (");
		for(int i=0,len=clusterCodes.size();i<len;i++) {
			sqlsb.append("'"+ clusterCodes.get(i) + "'");
			if (i<(len-1)) {
				sqlsb.append(",");
			}
		}
		sqlsb.append(")");
		Long count = super.getJdbcTemplate().queryForObject(sqlsb.toString(), new RowMapper<Long>() {
			@Override
			public Long mapRow(ResultSet rs, int rowNum) throws SQLException {

				return rs.getLong("count");
			}
		});
		return count;
	}

}
